from db import db
from datetime import datetime

class FeedingRecord(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    date = db.Column(db.Date)
    time = db.Column(db.Time)
    amount = db.Column(db.Integer)

class BabyInfo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    birthdate = db.Column(db.DateTime)

# 辅食类别表
class FoodCategory(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False)  # 主食、蔬菜、肉、海鲜、水果
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    # 关联到食物选项
    food_options = db.relationship('FoodOption', backref='category', lazy=True)

# 辅食选项表
class FoodOption(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)  # 具体食物名称
    category_id = db.Column(db.Integer, db.ForeignKey('food_category.id'), nullable=False)
    is_custom = db.Column(db.Boolean, default=False)  # 是否为用户自定义
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

# 辅食记录主表
class ComplementaryFoodRecord(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    date = db.Column(db.Date, nullable=False)
    time = db.Column(db.Time, nullable=False)
    acceptance_level = db.Column(db.String(20), nullable=False)  # 喜欢、一般、不喜欢、过敏
    notes = db.Column(db.Text)  # 备注
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    # 关联到辅食记录详情
    details = db.relationship('ComplementaryFoodDetail', backref='record', lazy=True, cascade='all, delete-orphan')

# 辅食记录详情表
class ComplementaryFoodDetail(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    record_id = db.Column(db.Integer, db.ForeignKey('complementary_food_record.id'), nullable=False)
    food_option_id = db.Column(db.Integer, db.ForeignKey('food_option.id'), nullable=False)
    
    # 关联到食物选项
    food_option = db.relationship('FoodOption', backref='details', lazy=True)